﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace Imagenator.Forms
{
	partial class DrivesForm : ArgusLib.Controls.BaseForm
	{
		public string DriveRoot { get; private set; }

		public DrivesForm()
		{
			InitializeComponent();
			this.lbDrives.Format += lbDrives_Format;
		}

		void lbDrives_Format(object sender, ListControlConvertEventArgs e)
		{
			DriveInfo drive = (DriveInfo)e.ListItem;
			try { e.Value = drive.VolumeLabel + " (" + drive.Name + ')'; }
			catch { e.Value = drive.Name; }
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.LoadDrives();
		}

		private void LoadDrives()
		{
			this.lbDrives.Items.Clear();
			DriveInfo[] drives = DriveInfo.GetDrives();
			foreach (DriveInfo drive in drives)
			{
				DriveType driveType = DriveType.Unknown;
				try { driveType = drive.DriveType; }
				catch { }
				if (driveType == DriveType.Removable || driveType == DriveType.CDRom)
					this.lbDrives.Items.Add(drive);
			}
			if (this.lbDrives.Items.Count < 1)
			{
				MessageBox.Show("No available removable drives were found.");
				this.Close();
			}
		}

		private void bImport_Click(object sender, EventArgs e)
		{
			if (this.lbDrives.SelectedIndex < 0)
			{
				MessageBox.Show("No Drive selected.", "Error");
				return;
			}
			try
			{
				DriveInfo drive = (DriveInfo)this.lbDrives.SelectedItem;
				this.DriveRoot = drive.RootDirectory.FullName;
			}
			catch (IOException exception)
			{
				MessageBox.Show(exception.Message, "Error");
				this.LoadDrives();
				return;
			}
			this.DialogResult = System.Windows.Forms.DialogResult.OK;
		}

		private void bCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}
	}
}
